package com.toommi.refuse.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toommi.refuse.common.enums.CommonStatusEnum;
import com.toommi.refuse.entity.*;
import com.toommi.refuse.enums.AdminExceptionEnum;
import com.toommi.refuse.exception.AdminException;
import com.toommi.refuse.mapper.SysPermissionMapper;
import com.toommi.refuse.mapper.SysRoleMapper;
import com.toommi.refuse.mapper.SysRolePermissionMapper;
import com.toommi.refuse.model.RolePermissionModel;
import com.toommi.refuse.service.admin.SysRoleService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangg
 * @since 2019-07-30 09:43
 */
@Service
@AllArgsConstructor
public class SysRoleServiceImpl implements SysRoleService {

    private SysRoleMapper roleMapper;

    private SysRolePermissionMapper rolePermissionMapper;

    private SysPermissionMapper permissionMapper;

    @Override
    public void addSysRole(SysRole role) throws AdminException {

        //校验角色名称

        if (StringUtils.isEmpty(role.getRoleName())) {
            throw new AdminException(AdminExceptionEnum.INVALID_SYS_ROLENAME_NULL);
        }
        SysRoleExample example = new SysRoleExample();
        example.createCriteria().andRoleNameEqualTo(role.getRoleName())
                .andStatusEqualTo(CommonStatusEnum.NORMAL.getCode());

        if (roleMapper.selectCountByExample(example) > 0) {
            throw new AdminException(AdminExceptionEnum.INVALID_SYS_ROLENAME_REPEAT);
        }

        role.setCreateTime(System.currentTimeMillis());
        role.setUpdateTime(System.currentTimeMillis());
        role.setStatus(CommonStatusEnum.NORMAL.getCode());

        roleMapper.insert(role);
    }

    @Override
    public void deleteSysRole(long id) {
        //角色做逻辑删除
        SysRole sysRole = roleMapper.selectByPrimaryKey(id);
        if (sysRole != null) {
            sysRole.setStatus(CommonStatusEnum.DELETED.getCode());
            roleMapper.updateByPrimaryKey(sysRole);
        }
    }

    @Override
    public void updateSysRole(SysRole sysRole) throws AdminException {
        //更新时允许更新角色名称
        SysRoleExample example = new SysRoleExample();
        example.createCriteria().andRoleNameEqualTo(sysRole.getRoleName())
                .andStatusEqualTo(CommonStatusEnum.NORMAL.getCode());
        SysRole role = roleMapper.selectOneByExample(example);

        if (role != null && !role.getId().equals(sysRole.getId())) {
            throw new AdminException(AdminExceptionEnum.INVALID_SYS_ROLENAME_REPEAT);
        }

        roleMapper.updateByPrimaryKey(sysRole);
    }

    @Override
    public PageInfo<SysRole> findSysRoles(int pageNum, int pageSize, String keyWord) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> sysRoleList;
        if (StringUtils.isEmpty(keyWord)) {
            SysRoleExample example = new SysRoleExample();
            example.createCriteria().andStatusEqualTo(CommonStatusEnum.NORMAL.getCode());
            sysRoleList = roleMapper.selectByExample(example);
        } else {
            SysRoleExample example = new SysRoleExample();
            example.createCriteria().andRoleNameLike("%" + keyWord + "%").andStatusEqualTo(CommonStatusEnum.NORMAL.getCode());
            sysRoleList = roleMapper.selectByExample(example);
        }

        return new PageInfo<>(sysRoleList);
    }

    @Override
    public void addRolePermission(RolePermissionModel model) throws AdminException {
        //角色是否存在
        long roleId = model.getRoleId();
        if (roleMapper.selectByPrimaryKey(roleId) == null) {
            throw new AdminException(AdminExceptionEnum.INVALID_SYS_ROLE_NULL);
        }

        SysRolePermissionExample example = new SysRolePermissionExample();
        example.createCriteria().andSysRoleIdEqualTo(roleId);
        rolePermissionMapper.deleteByExample(example);

        List<Long> permissionIds = model.getPermissionId();
        for (Long permissionId : permissionIds) {
            if (permissionMapper.selectByPrimaryKey(permissionId) == null) {
                //权限id信息错误
                throw new AdminException(AdminExceptionEnum.INVALID_SYS_PERMISSION);
            }
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setSysRoleId(roleId);
            sysRolePermission.setSysPermissionId(permissionId);
            rolePermissionMapper.insert(sysRolePermission);
        }

    }

    @Override
    public List<SysPermission> findRolePermissions(long roleId) throws AdminException {
        if (roleMapper.selectByPrimaryKey(roleId) == null) {
            throw new AdminException(AdminExceptionEnum.INVALID_SYS_ROLE_NULL);
        }

        SysRolePermissionExample sysRolePermissionExample = new SysRolePermissionExample();
        sysRolePermissionExample.createCriteria().andSysRoleIdEqualTo(roleId);

        List<SysRolePermission> rolePermissions = rolePermissionMapper.selectByExample(sysRolePermissionExample);

        if (rolePermissions == null || rolePermissions.isEmpty()) {
            return new ArrayList<>();
        }

        List<SysPermission> permissionList = new ArrayList<>();
        for (SysRolePermission sysRolePermission : rolePermissions) {
            SysPermission sysPermission = permissionMapper.selectByPrimaryKey(sysRolePermission.getSysPermissionId());
            if (sysPermission != null) {
                permissionList.add(sysPermission);
            }
        }
        return permissionList;
    }
}
